<?php
	function ten_arg_sort($prepare_data)
	{
		ksort($prepare_data);
		return $prepare_data;
	}
	function tenpay_build_mysign($prepare_data,$key,$md5) 
	{
		ten_arg_sort($prepare_data);
		extract($prepare_data);
		$signPars = "cmdno=" . $cmdno . "&" .
				"date=" . $date . "&" .
				"bargainor_id=" . $bargainor_id . "&" .
				"transaction_id=" . $transaction_id . "&" .
				"sp_billno=" . $sp_billno . "&" .
				"total_fee=" . $total_fee . "&" .
				"fee_type=" . $fee_type . "&" .
				"return_url=" . $return_url . "&" .
				"attach=" . $attach . "&";
		
		if($spbill_create_ip != "") {
			$signPars .= "spbill_create_ip=" . $spbill_create_ip . "&";
		}
		
		$signPars .= "key=" . $key;
		$sign = strtolower(md5($signPars));
	   // echo $sign."<br>";
	   // echo $signPars."<br>";

		return $sign;
	}
	function tenpay_build_mysign_2($prepare_data,$key,$md5) 
	{
		$signParameterArray = array(
			'attach',
			'chnid',
			'cmdno',
			'encode_type',
			'mch_desc',
			'mch_name',
			'mch_price',
			'mch_returl',
			'mch_type',
			'mch_vno',
			'need_buyerinfo',
			'seller',
			'show_url',
			'transport_desc',
			'transport_fee',
			'version'
		);
		
		ten_arg_sort($signParameterArray);
		//extract($prepare_data);
		foreach($signParameterArray as $k ) {
			$v = $prepare_data[$k];
			
			if($v) {
			//echo $k."=>".$v."<br/>";
				$signPars .= $k . "=" . urldecode($v) . "&";
			}
		}
		
		$signPars .= "key=" . $key;
		$sign = strtolower(md5($signPars));
	   // echo $sign."<br>";
	   // echo $signPars."<br>";

		return $sign;
	}
	function isTenpaySign($prepare_data,$key,$md5) {
		extract($prepare_data);
		$signPars = "";
		//组织签名串
		$signPars = "cmdno=" . $cmdno . "&" .
				"pay_result=" . $pay_result . "&" .
				"date=" . $date . "&" .
				"transaction_id=" . $transaction_id . "&" .
				"sp_billno=" . $sp_billno . "&" .
				"total_fee=" . $total_fee . "&" .
				"fee_type=" . $fee_type . "&" .
				"attach=" . $attach . "&" .
				"key=" . $key;
				
		$sign = strtolower(md5($signPars));

		return $sign;
		
	}
	function isTenpaySign_2($prepare_data,$key,$md5) {
		extract($prepare_data);
		$signParameterArray = array(
			'attach',
			'buyer_id',
			'cft_tid',
			'chnid',
			'cmdno',
			'mch_vno',
			'retcode',
			'seller',
			'status',
			'total_fee',
			'trade_price',
			'transport_fee',
			'version'
		);
		
		//按字母a-z排序
		ksort($signParameterArray);
		
		foreach($signParameterArray as $k ) {
			$v = $prepare_data[$k];
			if(isset($v)) {
				//echo $k."=>".$v."<br/>";

				$signPars .= $k . "=" . urldecode($v) . "&";
			}
		}
		
		$signPars .= "key=" . $key;
		
		$sign = strtolower(md5($signPars));
		return $sign;
		
	}
?>